<input type="hidden" value="__PAGESIZE__" id="page_size"/>
<input type="hidden" value="1" id="page_num"/>
<input type="hidden" value="1" id="pid"/>
<div class="row" style="margin-bottom:10px;margin-top:10px;">
    <div class="col-xs-12">
        <button id="add" type="button" class="btn btn-block btn-info btn-sm" style="width:100px;">添 加</button>
    </div>
</div>
<div class="row">
    <div class="col-xs-3" style="padding-right:0px;">
        <div class="box box-success" style="min-height:500px;">
            <div id="tree" class="ztree" style="padding-top:12px;padding-bottom:12px;"></div>
        </div>
    </div>
    <div class="col-xs-9">
        <div class="box box-danger">
            <div class="box-header">
                <h3 class="box-title"></h3>
                <div class="box-tools">
                    <div class="input-group input-group-sm" style="width: 250px;">
                        <input type="text" id="keyword" name="keyword" class="form-control pull-right" placeholder="搜索">
                        <div class="input-group-btn">
                            <button id="search" type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body table-responsive no-padding">
                <table class="table table-striped table-hover">
                    <tbody id="row_data">
                    </tbody>
                </table>
            </div>
            <!-- /.box-body -->
            <div class="box-footer clearfix">
                <div id="info_data" class="col-sm-5" style="top:5px;"><div class="dataTables_info" id="example1_info" role="status" aria-live="polite"></div></div>
                <div class="col-sm-7">
                    <ul id="paging" class="pagination pagination-sm no-margin pull-right"></ul>
                </div>
            </div>
            <div class="overlay hidden">
                <i class="fa fa-refresh fa-spin"></i>
            </div>
        </div>
        <!-- /.box -->
    </div>
</div>
<script type="text/javascript">
//模块名称定义
var moduleName = 'menu';

//树加载
var setting = {
    async: {
        enable: true,
        url:'__ADMIN__/' + moduleName + '/getParentMenu',
        autoParam:["id", "name", "level"],
        otherParam:{"otherParam":"zTreeAsyncTest"},
        dataFilter: filter
    },
    callback: {
        onClick: zTreeOnClick
    }
};

function filter(treeId, parentNode, childNodes) {
    if (!childNodes) return null;
    for (var i=0, l=childNodes.length; i<l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
    }
    return childNodes;
}

$(document).ready(function(){
    $.fn.zTree.init($("#tree"), setting);
});

function zTreeOnClick(event, treeId, treeNode) {
    //alert(treeNode.tId + ", " + treeNode.name);
    $('#pid').val(treeNode.tId.replace('tree_', ''));
    load();
};
// ~树加载

//加载数据
function load() {
    showLoad();
    $.ajax({
        type : "post",
        url : '__ADMIN__/' + moduleName + '/tolist',
        dataType:"json",
        data: {
            page_num : $('#page_num').val()
            ,page_size : $('#page_size').val()
            ,keyword : $('#keyword').val()
            ,pid : $('#pid').val()
            //多条件查询在此添加提交数据
        },
        success : function(msg) {
            showData(msg);
            paging_handler(msg);
            hideLoad();
        }
    });
}
load();

//搜索框事件
$('#search').on('click', function(e) {
    $('#page_num').val('1');
    load();
});
$('#keyword').on('keydown', function(e){
    if(e.keyCode == 13){
        $('#page_num').val('1');
        load();
    }
});
// ~搜索框事件

//根据页码显示数据
function showPage(pageNum) {
    $('#page_num').val(pageNum);
    load();
}

//删除
function delete_data(obj, id) {
    //询问框
    layer.confirm('您确认要删除该数据吗？', {
        btn: ['确认','取消'] //按钮
    }, function(index){
        showLoad();
        $.ajax({
            type : "post",
            url : '__ADMIN__/' + moduleName + '/delete',
            dataType:"json",
            data: {
                id : id,
            },
            success : function(msg) {
                //alert(msg.code);
                if (msg.code == 0) {
                    $(obj).parent().parent().fadeOut("slow");
                    //layer.msg('删除成功');
                    $.scojs_message('删除成功', $.scojs_message.TYPE_OK);
                } else {
                    //layer.msg('删除失败');
                    $.scojs_message('删除失败', $.scojs_message.TYPE_ERROR);
                }
                hideLoad();
            }
        });

        layer.close(index);
    }, function(){
    });
}

//添加
$('#add').on('click', function() {
    window.location.href = '__ADMIN__#' + moduleName + '/addoredit';
});

//编辑
function edit_data(id) {
    window.location.href = '__ADMIN__#' + moduleName + '/addoredit/id/' + id;
}

//状态改变
function changeState(obj) {
    var id = $(obj).attr('data-id');
    var state = $(obj).attr('state');
    //alert(id + ',' + state);
    showLoad();
    $.ajax({
        type : "post",
        url : '__ADMIN__/' + moduleName + '/changeState',
        dataType:"json",
        data: {
            id : id,
            isUse : state
        },
        success : function(msg) {
            hideLoad();
            if (msg == 1) {
                //alert($('.badge .bg-green').html());
                if (state == 2) {
                    $('#state_lab' + id).removeClass('bg-green');
                    $('#state_lab' + id).addClass('bg-danger');
                    $('#state_lab' + id).text('停用');
                    $(obj).removeClass('label-danger');
                    $(obj).addClass('label-success');
                    $(obj).attr('state', '1');
                    $(obj).text('启用');
                    $.scojs_message('菜单已禁止', $.scojs_message.TYPE_OK);
                } else if (state == 1) {
                    $('#state_lab' + id).removeClass('bg-danger');
                    $('#state_lab' + id).addClass('bg-green');
                    $('#state_lab' + id).text('正常');
                    $(obj).removeClass('label-success');
                    $(obj).addClass('label-danger');
                    $(obj).attr('state', '2');
                    $(obj).text('停用');
                    $.scojs_message('菜单已开启', $.scojs_message.TYPE_OK);
                }
            }
        }
    });
}

//数据拼装
function showData(msg) {
    var content = '';
    //datatable head
    content += '<tr>';
    content += '<th style="width:35px;">#</th>';
    content += '<th>菜单名称</th>';
    content += '<th>链接地址</th>';
    content += '<th>状态</th>';
    content += '<th>排序</th>';
    content += '<th>消息框数量</th>';
    content += '<th style="width:110px;">操作</th>';
    content += '</tr>';

    //datatable data
    for (var i = 0; i < msg.list.length; i++) {
        content += '<tr>';
        content += '<td>' + (i+1) + '</td>';
        content += '<td>' + msg.list[i].name + '</td>';
        content += '<td>' + msg.list[i].page_url + '</td>';
        if (msg.list[i].isuse == 1)
            content += '<td><span id="state_lab' + msg.list[i].id + '" class="badge bg-green">正常</span></td>';
        else
            content += '<td><span id="state_lab' + msg.list[i].id + '" class="badge bg-danger">停用</span></td>';
        content += '<td>' + msg.list[i].sort + '</td>';
        content += '<td>' + msg.list[i].msg_num + '</td>';
        content += '<td>';
        if (msg.list[i].isuse == 1)
            content += '<span class="label label-danger" style="cursor:pointer;margin-right:3px;" data-id="' + msg.list[i].id + '" state="2" onclick="changeState(this);">停用</span>';
        else
            content += '<span class="label label-success" style="cursor:pointer;margin-right:3px;" data-id="' + msg.list[i].id + '" state="1" onclick="changeState(this);">启用</span>';
        content += '<span class="label label-primary" style="cursor:pointer;margin-right:3px;" onclick="edit_data(' + msg.list[i].id + ');">修改</span>';
        content += '<span class="label label-warning" style="cursor:pointer;margin-right:3px;" onclick="delete_data(this,' + msg.list[i].id + ');">删除</span>';
        content += '</td>';
        content += '</tr>';
    }
    $('#row_data').html(content);
}
</script>